/*
 * Wave.cpp
 *
 *  Created on: 04/03/2013
 *      Author: eusyar
 */

#include "game/entities/Wave.h"
#include "core/GameObject.h"
#include "util/EatsVars.h"

using namespace eats_vars;

Wave::Wave(SDL_Surface * workingSurface, int _timeBetweenEnemiesLines,int _timeBetweenEnemies):GameObject(workingSurface) {
	waveSettings = new WaveSettings;
	waveSettings->timeBetweenEnemies=_timeBetweenEnemies;
	waveSettings->timeBetweenEnemiesLines = _timeBetweenEnemiesLines;

	_isActive = true;
	_oldTime = SDL_GetTicks();
	_actualEnemie=0;
	_nextIndexTarget=0;
}

Wave::~Wave() {

}

bool Wave::setUp()
{
	bool ret = true;
	for(int i=0; i< enemiesList.size(); i++){
		ret = ret && enemiesList.at(i)->setUp();
	}
	return ret;
}

bool Wave::update()
{
	if(!_isActive){
		return false;
	}

	if(_oldTime + waveSettings->timeBetweenEnemies < SDL_GetTicks() && _actualEnemie < enemiesList.size() ){
		displayEnemiesList.push_back(enemiesList.at(_actualEnemie));
		_actualEnemie ++;
		_oldTime = SDL_GetTicks();
	}

	bool ret = true;

	for(int i= 0; i < _actualEnemie; i++){
		if(displayEnemiesList.at(i)->_isAlive == true){
			ret = ret && displayEnemiesList.at(i)->update();
		}
	}
	return ret;
}

bool Wave::render()
{
	if(!_isActive){
		return false;
	}

	for(int i= 0; i < _actualEnemie; i++){
		if(displayEnemiesList.at(i)->_isAlive == true){
			displayEnemiesList.at(i)->render();
		}
	}
	return true;
}

bool Wave::tearDown()
{
	bool ret = true;
	cout << "|INFO| Iniciando a liberação dos objetos da wave..." << endl;
	for(int i=0; i< enemiesList.size(); i++){
		cout << "|INFO| Objeto a ser retirado da wave = "<< i << endl;
		ret = ret && enemiesList.at(i)->tearDown();
	}
	delete(waveSettings);
}


WaveHolder::WaveHolder(SDL_Surface * workingSurface): GameObject(workingSurface)
{

}

bool WaveHolder::setUp()
{
	bool ret = true;
	for(int i= 0; i < waveVactor.size(); i++){
		ret = ret && waveVactor.at(i)->setUp();
		enemiesList.insert(enemiesList.end(), waveVactor.at(i)->enemiesList.begin(),waveVactor.at(i)->enemiesList.end());
	}
	return ret;
}

bool WaveHolder::update()
{
	bool ret = true;
	for(int i= 0; i < waveVactor.size(); i++){
		ret = ret && waveVactor.at(i)->update();
	}
	return ret;
}

bool WaveHolder::render()
{
	bool ret = true;
	for(int i= 0; i < waveVactor.size(); i++){
		//cout << "Tamso ae" << endl;
		ret = ret && waveVactor.at(i)->render();
	}
	return ret;
}

bool WaveHolder::tearDown()
{
	bool ret = true;
	cout << "|INFO| Iniciando a liberação dos objetos da wave holder..." << endl;
	for(int i= 0; i < waveVactor.size(); i++){
		cout << "|INFO| Objeto da wave holder "<< i << endl;
		ret = ret && waveVactor.at(i)->tearDown();
	}
	return ret;
}

vector<BaseEnemy *> WaveHolder::getEnemiesList()
{
	//cout << enemiesList.size() << endl;
	return enemiesList;
}

